<?php
/**
 * Created by PhpStorm.
 * User: Rei
 * Date: 16/5/1
 * Time: 13:35
 * Mail: rei@king.moe
 */

namespace Admin\Controller;

use Think\Controller;

class NewsController extends SessionController
{
    public function index()
    {
        $this->display('news/news');
    }

    public function publish(){
        $tags = M('topic')->where('')->select();
        $this->assign('tags', $tags);
        $this->display('news/news_form');
    }

    //添加新闻
    public function add(){
        $data = array();
        $relink = array();

        $data['title'] = I('post.title');
        $data['content'] = $_POST['content'];
        $data['publisher'] = I('post.publisher');
        $data['topic'] = I('post.tag');

        $relink['relink'] = I('post.relink');
        $relink['resource'] = I('post.resource');
        $relink = json_encode($relink);
        $data['relink'] = $relink;
        $data['pubtime'] = time();

        $news = D('News');
        $result = $news->addNews( $data );
        if($result){
            echo json_encode(array('code' => 1 , 'message' => '发布成功'));
        }else{
            echo json_encode(array('code' => 0 , 'message' => '发布失败'));
        }
    }


    //插入图片处理
    public function addPic()
    {

        header("Content-type:text/html");
        import('ORG.Net.UploadFile');

        //文件上传
        $config = array(
            'rootPath' => './Public/',
            'savePath' => 'upload/news/',
            'exts'     => array('jpg', 'gif', 'png', 'jpeg'),// 设置附件上传类型
            'saveRule' => 'uniqid'
        );

        $upload = new \Think\Upload($config);    //实例化上传类，顺便加载配置
        $res = $upload -> upload();
        if(!$res){
            echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(" . I('get.CKEditorFuncNum') . ", '/', '上传失败," . $upload->getError() . "！');</script>";
            return false;
        }
        else{
            //// 上传成功 获取上传文件信息并存入数据库
            $savepath = __ROOT__.'/Public/'.$res['upload']['savepath'] . $res['upload']['savename'];
            //下面的输出，会自动的将上传成功的文件路径，返回给编辑器。
            echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(" . I('get.CKEditorFuncNum') . ",'$savepath','');</script>";
        }
        return true;
    }


    //新闻列表分页数据
    public function newsList()
    {
        $sEcho = I('post.sEcho', 0, 'intval');            //DataTables 用来生成的信息
        $start = I('post.iDisplayStart', 0, 'intval');    //显示的起始索引
        $length = I('post.iDisplayLength', 0, 'intval');   //每页显示的行数
        $sort_th = I('post.iSortCol_0', 2, 'intval');       //排序的列 默认第三列
        $sort_type = I('post.sSortDir_0', 'desc');  //排序的方向 默认 desc

        //@TODO 添加过滤规则
        $search = I('post.sSearch', '');         //全局搜索关键字 默认为空

        $News = D('news');

//        if ($sort_th == 1) {
//            $order_key = "pubtime";
//        } else {
//            $order_key = "end_time";
//        }

        $order_key = 'pubtime';

        $data['aaData'] = $News->where("title LIKE '%s'", '%' . $search . '%')
            ->field('nid,title,publisher,pubtime')
            ->order("$order_key " . $sort_type)
            ->limit($start, $length)
            ->select();

        //查询条数
        $total = count($News->where("title LIKE '%s'", '%' . $search . '%')->select()); //总数

        $data['sEcho'] = $sEcho;

        $data['iTotalDisplayRecords'] = $total;

        $data['iTotalRecords'] = $total;

        $this->ajaxReturn($data);
    }

}